package lesson.score;

import lesson.base.BaseService;
import lesson.db.DbUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class ScoreService extends BaseService {


    public String save(List<Score> scores) {
        if (scores.isEmpty()) {
            return "请填写分数再保存";
        }
        SqlSession session = DbUtil.getSession(false);
        try {
            Statement statement = session.getConnection().createStatement();
            statement.execute("SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try{
            ScoreDao dao = session.getMapper(ScoreDao.class);
            dao.saveScores(scores);
            dao.calTotalScore(scores.get(0).getExamId());

            dao.updateClassRank(scores.get(0).getExamId());
            dao.updateGradeRank(scores.get(0).getExamId());

            session.commit();
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {

            session.close();
        }

        return null;

    }

    public List<Score> listStudentScores(Integer examId, Integer classId) {
        SqlSession session = DbUtil.getSession(true);
        try{
            ScoreDao dao = session.getMapper(ScoreDao.class);
            return dao.listScores(examId, classId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.close();
        }
        return null;
    }

    public List<StatisticsBean> listGradeStatisticsData(Integer examId, Integer gradeId, Integer subjectId) {
        SqlSession session = DbUtil.getSession(true);
        try {
            Statement statement = session.getConnection().createStatement();
            statement.execute("SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try{
            ScoreDao dao = session.getMapper(ScoreDao.class);
            return dao.listGradeStatisticsData(examId, gradeId, subjectId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.close();
        }
        return null;
    }

    /**
     * 班级某个学科的统计数据
     * @param classId
     * @param subjectId
     * @return
     */
    public List<StatisticsBean> listClassStatisticsData(Integer classId, Integer subjectId) {
        SqlSession session = DbUtil.getSession(true);
        try {
            Statement statement = session.getConnection().createStatement();
            statement.execute("SET sql_mode ='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try{
            ScoreDao dao = session.getMapper(ScoreDao.class);
            return dao.listClassStatisticsData(classId, subjectId);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            session.close();
        }
        return null;
    }
}
